package com.tgy.core.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tgy.core.mapper.SysAdminRoleMapper;
import com.tgy.core.service.SysAdminRoleService;
import com.tgy.page.PageQuery;
import com.tgy.page.TableDataInfo;
import com.tgy.pojo.SysAdminRole;
import com.tgy.result.LayuiTableResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

/**
 * 统用户角色关联信息Service业务层处理
 *
 * @author 自动生成
 * @date 2022-06-20
 */
@Service
public class SysAdminRoleServiceImpl extends ServiceImpl<SysAdminRoleMapper, SysAdminRole> implements SysAdminRoleService {

    @Resource
    private SysAdminRoleMapper baseMapper;

    /**
     * 查询统用户角色关联信息
     *
     * @param id 统用户角色关联信息主键
     * @return 统用户角色关联信息
     */
    @Override
    public SysAdminRole queryById(String id){
        return baseMapper.selectById(id);
    }

    /**
     * 查询统用户角色关联信息列表
     *
     * @param bo 统用户角色关联信息
     * @return 统用户角色关联信息
     */
    @Override
    public TableDataInfo<SysAdminRole> queryPageList(SysAdminRole bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysAdminRole> lqw = buildQueryWrapper(bo);
        Page<SysAdminRole> result = baseMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public LayuiTableResult queryLayuiPageList(SysAdminRole bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysAdminRole> lqw = buildQueryWrapper(bo);
        Page<SysAdminRole> result = baseMapper.selectPage(pageQuery.build(), lqw);
        return new LayuiTableResult(result.getTotal(),result.getRecords());
    }

    /**
     * 查询统用户角色关联信息列表
     *
     * @param bo 统用户角色关联信息
     * @return 统用户角色关联信息
     */
    @Override
    public List<SysAdminRole> queryList(SysAdminRole bo) {
        LambdaQueryWrapper<SysAdminRole> lqw = buildQueryWrapper(bo);
        return baseMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<SysAdminRole> buildQueryWrapper(SysAdminRole bo) {
        LambdaQueryWrapper<SysAdminRole> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotEmpty(bo.getAid()),SysAdminRole::getAid,bo.getAid());
        lqw.eq(StrUtil.isNotEmpty(bo.getRid()),SysAdminRole::getRid,bo.getRid());
        return lqw;
    }

    /**
     * 新增统用户角色关联信息
     *
     * @param bo 统用户角色关联信息
     * @return 结果
     */
    @Override
    public Boolean insertByBo(SysAdminRole bo) {
        // validEntityBeforeSave(SysAdminRole);
        boolean flag = baseMapper.insert(bo) > 0;
        if (flag) {
            bo.setId(bo.getId());
        }
        return flag;
    }

    /**
     * 修改统用户角色关联信息
     *
     * @param bo 统用户角色关联信息
     * @return 结果
     */
    @Override
    public Boolean updateByBo(SysAdminRole bo) {
        // PBizPatient update = BeanUtil.toBean(bo, PBizPatient.class);
        // validEntityBeforeSave(SysAdminRole);
        return baseMapper.updateById(bo) > 0;
    }

    @Override
    public Boolean deleteById(String id) {
        return baseMapper.deleteById(id) > 0;
    }


    /**
     * 批量删除统用户角色关联信息
     *
     * @param ids 需要删除的统用户角色关联信息主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        /*if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }*/
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 根据用户ID删除数据
     */
    @Override
    public int deleteByAid(List<String> aids) {
        return baseMapper.deleteByAid(aids);
    }

    @Override
    public int deleteByRid(List<String> rids) {
        return baseMapper.deleteByRid(rids);
    }

    @Override
    public int insertByList(List<SysAdminRole> adminRoles) {
        return baseMapper.insertByList(adminRoles);
    }


}
